class VueAuth {

  constructor() {
    this.permissions = [];
    this.roles = [];
  }

  /**
   * 设置角色
   * @param roles
   */
  setRoles = roles => {
    console.log("设置用户角色以及权限");
    console.log(roles);
    this.roles = roles;
    roles.forEach(item => {
      if (item.permissions) {
        this.addPermissions(item.permissions)
      }
    })
  };

  /**
   * 设置权限
   * @param permissions
   */
  setPermissions = permissions => {
    this.permissions = permissions
  };

  addPermissions = permissions => {
    if (!permissions) return;
    if (!this.permissions) {
      this.permissions = []
    }
    this.permissions = [...this.permissions, ...permissions];
  };

  /**
   * 检查用户角色
   * @param role
   */
  checkRole = role => {
    for (let i = 0; i < this.roles.length; i++) {
      let item = this.roles[i];
      if (item == role || item.role_name == role || item.roleName == role) {
        return true
      }
    }
    return false
  };

  haveRoles = roles => {
    for (let index in roles) {
      if (roles.hasOwnProperty(index)) {
        if (this.checkRole(roles[index])) {
          return true
        }
      }
    }
    return false
  };

  havePermissions = permissions => {

    for (let index in permissions) {
      if (permissions.hasOwnProperty(index)) {
        if (this.checkPermission(permissions[index])) {
          return true
        }
      }
    }
    return false
  };

  /**
   * 检查是否拥有权限
   * @param permission
   * @returns {boolean}
   */
  checkPermission = permission => {
    for (let i = 0; i < this.permissions.length; i++) {
      let item = this.permissions[i];
      if (item == permission || item.permission_name == permission || item.permissionName == permission) {
        return true
      }
    }
    return false
  };

  filterRouter = routes => {
    if (!routes || routes.length <= 0) return;
    let _routes = [];
    for (let i in routes) {
      if (routes.hasOwnProperty(i)) {

        let route = routes[i];

        if (route.children && route.children.length > 0) {
          route.children = this.filterRouter(route.children)
        }

        if (route.meta && route.meta.auth) {
          let auth = route.meta.auth;

          if (auth) {
            let haveRole = true;
            if (auth.roles) {
              haveRole = this.haveRoles(auth.roles)
            }

            let havePermission = true;

            if (auth.permissions) {
              havePermission = this.havePermissions(auth.permissions);
            }

            console.log(route);
            console.log("haveRole:" + haveRole);
            console.log("havePermission:" + havePermission);

            if (haveRole && havePermission) {
              _routes = [..._routes, route]
            }

          } else {
            _routes = [..._routes, route]
          }
        } else {
          _routes = [..._routes, route]
        }
      }
    }
    return _routes
  };

  getToken = () => {
    return window.localStorage.getItem("token");
  };

  setToken = (token) => {
    window.localStorage.setItem("token", token);
  };

  removeToken = () => {
    window.localStorage.removeItem("token");
  };

  setRoutes = (routes) => {
    window.localStorage.setItem("routes", JSON.stringify(routes));
  };

  getRoutes = () => {
    let routes = window.localStorage.getItem("routes");
    return JSON.parse(routes);
  }

}

export const auth = new VueAuth();
